﻿--[[
   @brief    离线模式tab面板
   @author   hqzheng
   @version  1.0
   @date     2017-04-12
--]]

navigation_tab = 
{
    mainframe_dialog_id = 0;
    parent_id = 0;
    home_tab_id = 0;
    shop_tab_id = 0;
    video_tab_id = 0;
    selected_flag_id = 0;

    cur_select_tab_id = 0;

    btn_info = {};

    NAV_HEIGHT = 80;
    MENU_OFFSET_X = 8;

    tab_type_2_tab_id = {};

    first_switch = true;

    is_english_ver = false;

    tab_ctn_ptr = nil;
    module_id_2_tab_id = {};

    init = function (parent_id, mainframe_dialog_id)
        navigation_tab.initEnglishVer();

        navigation_tab.mainframe_dialog_id = mainframe_dialog_id;
        navigation_tab.parent_id = parent_id;
        navigation_tab.selected_panel_id = ui_utility.get_ctrl_id_by_path(
            mainframe_dialog_id, "pnlSelected");
        navigation_tab.selected_flag_id = ui_utility.get_ctrl_id_by_path(
            mainframe_dialog_id, "pnlSelected\\tabSelected");
        navigation_tab.home_tab_ani_id = ui_utility.get_ctrl_id_by_path(parent_id, "CtnHomeAni");
        local plugin_info_path = "offline\\UIConfig\\lua_plugins_info.xml";

        if base_utility.CLIENT_TYPE.OVERSEA_VER == base_utility.get_client_version_type() then
            plugin_info_path = "offline\\UIConfig\\lua_plugins_info_overseas.xml";
        end

        local node_count = lua_plugin_mgr.add_module_plugins("navigation_tab", plugin_info_path);
        local ctn_width = node_count * 102;
        ui_utility.setproperty_by_id(navigation_tab.parent_id, "Width", ctn_width);
        ui_utility.setproperty_by_id(navigation_tab.selected_panel_id, "Width", ctn_width);
        local cnt_id = ui_utility.get_ctrl_id_by_path(parent_id, "Container_1");
        ui_utility.setproperty_by_id(cnt_id, "Transparent", "true");
        local scrl_view_id = ui_utility.get_ctrl_id_by_path(cnt_id, "ScrollView_1");
        ui_utility.setproperty_by_id(scrl_view_id, "Transparent", "true");
        local inner_ctn_id = ui_utility.get_ctrl_id_by_path(scrl_view_id, "Container_2");
        ui_utility.setproperty_by_id(inner_ctn_id, "Transparent", "true");
        ui_utility.setproperty_by_id(navigation_tab.parent_id, "ColumnCount", node_count);
        navigation_tab.tab_ctn_ptr = ui_utility.get_ctrl_ptr_by_id(navigation_tab.parent_id);
        if nil ~= navigation_tab.tab_ctn_ptr then
            navigation_tab.tab_ctn_ptr:ClearAllItems();
        end

        lua_plugin_mgr.load_plugins("navigation_tab");
        navigation_tab.switch_offline_tab(GAME_ID.GAEM_ID_HOME_PAGE, true);

        comm_center.reg_service("i_service_offline_switch_tab", 
            navigation_tab.handle_switch_tab_service);
    end;

    initEnglishVer = function()
        local tpfui = base_utility.get_component_adapter("ITpf_ui", "ierd_tgp.tpf_ui.GetTPFUI();");
        if tpfui ~= nil then
            navigation_tab.is_english_ver = tpfui:Is_English_ver();
        end;
    end;

    inner_log = function (msg, core_msg)
        base_utility.log(string.format("[navigation_tab] %s", msg), core_msg);
    end;

    show = function (is_show)
        ui_utility.set_visible(navigation_tab.parent_id, is_show);
    end;

    switch_offline_tab = function (tab_type, is_defual_select)
        base_utility.log("switch_tab", true);
        local target_tab_id = navigation_tab.module_id_2_tab_id[tab_type];
        if target_tab_id == navigation_tab.cur_select_tab_id then
            return;
        end

        navigation_tab.set_btn_slct_status(target_tab_id);
        navigation_tab.set_btn_nor_status(navigation_tab.cur_select_tab_id, true);
        navigation_tab.cur_select_tab_id = target_tab_id;

        navigation_tab.move_select_flag(navigation_tab.cur_select_tab_id, false);

        if nil == is_defual_select or false == is_defual_select then
            -- tab切换广播
            local info = {nav_tab_type = tab_type}
            local tab_info = navigation_tab.btn_info[target_tab_id];
            if nil ~= tab_info then
                info.moudle_id = tab_info.module_id;
            end
            comm_center.send_broadcast("i_broadcast_nav_tab_switch", info);
        end
    end;

    set_btn_nor_status = function (btn_id)
        local btn_info = navigation_tab.btn_info[btn_id];
        if nil == btn_info then
            return;
        end

        local tab_txt_id = ui_utility.get_ctrl_id_by_path(btn_id, "lblText");
        local inner_btn_id = ui_utility.get_ctrl_id_by_path(btn_id, "btnIcon");
        local ani_ctn_id = ui_utility.get_ctrl_id_by_path(btn_id, "AniCtn");

        ui_utility.setproperty_by_id(inner_btn_id, "Background", "NONE");
        if navigation_tab.is_english_ver then
            ui_utility.setproperty_by_id(ani_ctn_id, "Background", btn_info.normal_en);
        else
            if true == is_need_ani then
                ui_utility.setproperty_by_id(ani_ctn_id, "Background", btn_info.un_slct_ani);
            else
                ui_utility.setproperty_by_id(ani_ctn_id, "Background", btn_info.nor_bkg);
            end;
        end;
    end;

    set_btn_slct_status = function (btn_id)
        local btn_info = navigation_tab.btn_info[btn_id];
        if nil == btn_info then
            return;
        end

        local tab_txt_id = ui_utility.get_ctrl_id_by_path(btn_id, "lblText");
        local inner_btn_id = ui_utility.get_ctrl_id_by_path(btn_id, "btnIcon");
        local ani_ctn_id = ui_utility.get_ctrl_id_by_path(btn_id, "AniCtn");

        ui_utility.setproperty_by_id(inner_btn_id, "Background", "NONE");
        if navigation_tab.is_english_ver then
            ui_utility.setproperty_by_id(ani_ctn_id, "Background", btn_info.press_en);
        else
            ui_utility.setproperty_by_id(ani_ctn_id, "Background", btn_info.slct_ani);
        end;
    end;

    cal_tab_offset = function (tab_id)
        local info = navigation_tab.btn_info[tab_id];
        if nil == info then
            return 0, 0; 
        end
        local offset_x = tonumber(info.tab_offset_x);
        local offset_y = tonumber(info.tab_offset_y);
        if nil == offset_x then
            offset_x = 0;
        end
        if nil == offset_y then
            offset_y = 0;
        end
        return offset_x, offset_y;
    end;

    move_select_flag = function (dst_tab_id, is_use_animation)
        local select_flag_ptr = ui_utility.get_ctrl_ptr_by_id(navigation_tab.selected_flag_id);
        if nil == select_flag_ptr then
            return;
        end

        navigation_tab.set_select_flag_visible(true);
        local select_flag_rc = select_flag_ptr:GetRectEx(TRUE,TRUE);
       
        local select_flag_w = select_flag_rc.right - select_flag_rc.left;
        local select_flag_h = select_flag_rc.bottom - select_flag_rc.top;
        local cur_tab_offset_x, cur_tab_offset_y = 
            navigation_tab.cal_tab_offset(navigation_tab.cur_select_tab_id);
        local dst_tab_offset_x, dst_tab_offset_y = navigation_tab.cal_tab_offset(dst_tab_id);

        if true == is_use_animation then
            -- 位移动画
            local start_pos = POINT:new_local();
            start_pos.x = cur_tab_offset_x;
            start_pos.y = cur_tab_offset_y;

            local end_pos = POINT:new_local();
            end_pos.x = dst_tab_offset_x;
            end_pos.y = dst_tab_offset_y;

            animation_utility.init(
                navigation_tab.selected_flag_id,
                "Select_Flag_Move", animation_utility.ANI_TYPE.ANI_MOVE);
            animation_utility.move_animation(
                navigation_tab.selected_flag_id, start_pos, end_pos, 8, 0);
        else
            select_flag_rc.left = dst_tab_offset_x;
            select_flag_rc.right = select_flag_rc.left + select_flag_w;
            select_flag_rc.top = dst_tab_offset_y;
            select_flag_rc.bottom = select_flag_rc.top + select_flag_h;
            select_flag_ptr:MoveEx(select_flag_rc,TRUE);
        end
    end;

--    on_click_home_tab = function (uilib, control_id)
--        navigation_tab.move_select_flag(navigation_tab.home_tab_id, true);

--        local jump_page = GAEM_ID_HOME_PAGE;
--        navigation_tab.open_tab_page(jump_page);
--        TOfflineView.set_list_panel_visible(true);
--    end;

--    on_click_shop_tab = function (uilib, control_id)
--        navigation_tab.move_select_flag(navigation_tab.shop_tab_id, true);
--        navigation_tab.open_tab_page(GAME_ID.GAME_ID_SHOP);
--        TOfflineView.set_list_panel_visible(false);
--    end;

--    on_click_video_tab = function (uilib, control_id)
--        navigation_tab.move_select_flag(navigation_tab.video_tab_id, true);
--        navigation_tab.open_tab_page(GAME_ID.GAME_ID_GAME_LIVE);
--        TOfflineView.set_list_panel_visible(false);
--    end;

    on_tab_mouse_hover = function (uilib, control_id)
        base_utility.log("on_tab_mouse_hover");
        local btn_tab_id = ui_utility.get_ctrl_parent_id(control_id);
        if navigation_tab.cur_select_tab_id ~= btn_tab_id then
            local inner_btn_id = ui_utility.get_ctrl_id_by_path(btn_tab_id, "btnIcon");
            local ani_ctn_id = ui_utility.get_ctrl_id_by_path(btn_tab_id, "AniCtn");
            local btn_info = navigation_tab.btn_info[btn_tab_id];
            ui_utility.setproperty_by_id(ani_ctn_id, "Background", "NONE");
            if navigation_tab.is_english_ver then
                ui_utility.setproperty_by_id(inner_btn_id, "Background", btn_info.hover_en);
            else
                ui_utility.setproperty_by_id(inner_btn_id, "Background", btn_info.nor_hover_ani);
            end;
        end
    end;

    on_tab_mouse_leave = function (uilib, control_id)
        local btn_tab_id = ui_utility.get_ctrl_parent_id(control_id);
        if navigation_tab.cur_select_tab_id ~= btn_tab_id then
            local btn_tab_id = ui_utility.get_ctrl_parent_id(control_id);
            local inner_btn_id = ui_utility.get_ctrl_id_by_path(btn_tab_id, "btnIcon");
            local ani_ctn_id = ui_utility.get_ctrl_id_by_path(btn_tab_id, "AniCtn");
            local btn_info = navigation_tab.btn_info[btn_tab_id];
            ui_utility.setproperty_by_id(ani_ctn_id, "Background", "NONE");
            if navigation_tab.is_english_ver then
                ui_utility.setproperty_by_id(inner_btn_id, "Background", btn_info.normal_en);
            else
                ui_utility.setproperty_by_id(inner_btn_id, "Background", btn_info.nor_bkg);
                ui_utility.setproperty_by_id(inner_btn_id, "Background", btn_info.nor_leave_ani);
            end;
        end
    end; 

    set_select_flag_visible = function (is_visible)
        ui_utility.set_visible(navigation_tab.selected_flag_id, is_visible);
    end;

    handle_switch_tab_service = function (data)
        if nil == data or nil == data.tab_type then
            return;
        end
        navigation_tab.switch_offline_tab(data.tab_type);
    end;

    is_mouse_in_menu_and_btn = function ()
        local menu_id = ui_utility.get_ctrl_id_by_path(
            navigation_tab.mainframe_dialog_id, "mnShop");
        local menu_ptr = ui_utility.get_ctrl_ptr_by_id(menu_id);
        local inner_btn_ptr = ui_utility.get_ctrl_ptr_by_id(navigation_tab.shop_tab_id);
        if nil == menu_ptr or nil == inner_btn_ptr then
            return false;
        end

        local rect_menu = menu_ptr:GetRectEx(TRUE,TRUE); 
        local point = ierd_tgp.tray_ui.Point:new_local();
        local tray_ui =
            base_utility.get_component_adapter("ITray_ui",
                "ierd_tgp.tray_ui.get_Tray_ui_adapter();");
        if tray_ui ~= nil then
            tray_ui:get_cursor_pos(point);
        end             
        local mouse_in = false;
        if rect_menu.left <= point.x and point.x <= rect_menu.right and
            rect_menu.top <= point.y and point.y <= rect_menu.bottom then
            mouse_in = true;
        end

        if not mouse_in then
            -- 判断是否在btn里
            local rect_btn = inner_btn_ptr:GetRectEx(TRUE,TRUE);
            if rect_btn.left <= point.x and point.x <= rect_btn.right and
                rect_btn.top <= point.y and point.y <= rect_btn.bottom then
                mouse_in = true;
            end
        end
        return mouse_in;
    end;

    open_tab_page = function (game_id)
        local param = {};
        local game_lib =
            base_utility.get_component_adapter("IGame_library",
                "ierd_tgp.game_library.Get_game_library_adapter();");
        if nil == game_lib then
            return;
        end
        navigation_tab.switch_offline_tab(game_id);
    end;

    proc_ui_node = function (ui_info_path)
        if nil == navigation_tab.tab_ctn_ptr then
            return;
        end
        local xml = newParser();
        local xmlparser = xml:loadVfsFile(ui_info_path);
        if nil == xmlparser or nil == xmlparser.ui_info then
            return false;
        end
        local item_ctrl_id = navigation_tab.tab_ctn_ptr:AddOneItem();
        local ui_info = {};
        ui_info["plugin_name"] = xmlparser.ui_info["@name"];

        if nil ~= xmlparser.ui_info.nor_bkg then
            ui_info["nor_bkg"] = xmlparser.ui_info.nor_bkg["@name"];
        end
        if nil ~= xmlparser.ui_info.nor_hover_ani then
            ui_info["nor_hover_ani"] = xmlparser.ui_info.nor_hover_ani["@name"];
        end
        if nil ~= xmlparser.ui_info.slct_ani then
            ui_info["slct_ani"] = xmlparser.ui_info.slct_ani["@name"];
        end
        if nil ~= xmlparser.ui_info.un_slct_ani then
            ui_info["un_slct_ani"] = xmlparser.ui_info.un_slct_ani["@name"];
        end
        if nil ~= xmlparser.ui_info.nor_leave_ani then
            ui_info["nor_leave_ani"] = xmlparser.ui_info.nor_leave_ani["@name"];
        end
        if nil ~= xmlparser.ui_info.tab_offset_x then
            ui_info["tab_offset_x"] = tonumber(xmlparser.ui_info.tab_offset_x["@name"]);
        end
        if nil ~= xmlparser.ui_info.tab_offset_y then
            ui_info["tab_offset_y"] = tonumber(xmlparser.ui_info.tab_offset_y["@name"]);
        end
        if nil ~= xmlparser.ui_info.module_id then
            ui_info["module_id"] = tonumber(xmlparser.ui_info.module_id["@name"]);
            navigation_tab.module_id_2_tab_id[tonumber(ui_info.module_id)] = item_ctrl_id;
            if GAME_ID.GAEM_ID_HOME_PAGE == tonumber(ui_info.module_id) then
                navigation_tab.home_tab_id = item_ctrl_id;
            end
        end
        if nil ~= xmlparser.ui_info.menu_path then
            ui_info["menu_path"] = xmlparser.ui_info.menu_path["@name"];
        end
        if nil ~= xmlparser.ui_info.normal_en then
            ui_info["normal_en"] = xmlparser.ui_info.normal_en["@name"];
        end
        if nil ~= xmlparser.ui_info.hover_en then
            ui_info["hover_en"] = xmlparser.ui_info.hover_en["@name"];
        end
        if nil ~= xmlparser.ui_info.press_en then
            ui_info["press_en"] = xmlparser.ui_info.press_en["@name"];
        end

        navigation_tab.btn_info[item_ctrl_id] = ui_info;
        navigation_tab.set_btn_nor_status(item_ctrl_id);
        local inner_btn_id = ui_utility.get_ctrl_id_by_path(item_ctrl_id, "btnIcon");
        ui_utility.setproperty_by_id(inner_btn_id, "OnClick", "navigation_tab.on_click_tab");
    end;

    on_click_tab = function (uilib, control_id)
        local tab_id = ui_utility.get_ctrl_parent_id(control_id);
        local btn_info = navigation_tab.btn_info[tab_id];
        if nil ~= btn_info then
            base_utility.log("APP Page Timing:" .. btn_info.module_id .. ",step1,on_click_tab");
            base_utility.mainframe_report("main_window",
                "mainframe_btn_click", btn_info.plugin_name);
            navigation_tab.move_select_flag(tab_id, true);
            if btn_info.module_id == GAME_ID.GAEM_ID_HOME_PAGE then
                TOfflineView.set_list_panel_visible(true);
                navigation_tab.open_tab_page(tonumber(btn_info.module_id));
            else
                TOfflineView.set_list_panel_visible(false);
                navigation_tab.open_tab_page(tonumber(btn_info.module_id));
            end
        end
    end;
};